package com.mmlc.meimei.lichengkongjian.customview;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.CateBean;
import com.mmlc.network.filter.FiltrateCategoryRequestFilter;
import com.mmlc.network.responsebean.FiltrateCategoryResponseBean;
import com.mmlc.utils.ReflectToBeanUtils;
import com.mmlc.utils.ToastUtils;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/29 0029.
 */
public class CategoryListView extends FrameLayout {
    private FrameLayout flback;

    private String cate_id;

    public CategoryListView(Context context) {
        super(context);
        init();
    }

    public CategoryListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private boolean allSelectable;

    public boolean isAllSelectable() {
        return allSelectable;
    }

    public void setAllSelectable(boolean allSelectable) {
        this.allSelectable = allSelectable;
    }

    public CategoryListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(21)
    public CategoryListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public List<String> groupData = new ArrayList<>();
    public List<List<String>> childData = new ArrayList<>();
    ListView listView;
    Myadapter myadapter;
    LinearLayout ll_categoryRecord;
    String keywork="";
    private void init() {
        setVisibility(View.GONE);

        View view = View.inflate(getContext(), R.layout.testexpand_layout, null);
        flback = (FrameLayout) view.findViewById(R.id.fl_back);
        flback.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                animOutView();
            }
        });
        ll_categoryRecord = (LinearLayout) view.findViewById(R.id.ll_categoryRecord);
        ll_categoryRecord.setVisibility(View.GONE);

        View fl_sure = view.findViewById(R.id.fl_sure);
        fl_sure.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (ll_categoryRecord.getChildCount() == 0) {//全部分类
                    if (selectListener != null) {
                        animOutView();
                        cate_id = "";
                        selectListener.onClicked("", "全部分类");
                        myadapter.notifyDataSetChanged();
                    }

                } else {//子分类
                    if (selectListener != null) {
                        View view = ll_categoryRecord.getChildAt(ll_categoryRecord.getChildCount() - 1);
                        CateBean cateBean = (CateBean) view.getTag();
                        animOutView();
                        cate_id = cateBean.id;
                        selectListener.onClicked(cateBean.id, cateBean.name);
                        myadapter.notifyDataSetChanged();
                    }
                }

            }
        });

        View all_category = view.findViewById(R.id.all_category);
        all_category.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               /* if (allSelectable) {
                    ll_categoryRecord.removeAllViews();
                    ll_categoryRecord.setVisibility(View.GONE);
                    setPid("");
                }*/

            }
        });
        listView = (ListView) view.findViewById(R.id.listView);
        myadapter = new Myadapter();
        listView.setAdapter(myadapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CateBean cateBean = uiData.get(position);
                if (cateBean.hasChild == 1) {

                    addCategory(uiData.get(position));
                    setPid(cateBean.id,"");
                } else {
                    animOutView();
                    cate_id = cateBean.id;
                    selectListener.onClicked(cateBean.id, cateBean.name);
                    myadapter.notifyDataSetChanged();
                }
            }
        });
        addView(view);
    }

    public void setInitData(String pid, String name,String brandId,String keyword) {
        CateBean data = new CateBean();
        data.id = pid;
        if (TextUtils.isEmpty(name))
            data.name = "全部分类";
        else
            data.name = name;
        data.hasChild = 1;
        this.keywork=keyword;
        addCategory(data);
        this.brandId=brandId;
        setPid(pid,keyword);
    }

    private String brandId;

    public void addCategory(CateBean data) {
        View view = View.inflate(getContext(), R.layout.view_categoryitem, null);

        TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
        tv_name.setText(data.name + "/");
        view.setTag(data);
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                CateBean data = (CateBean) v.getTag();
                setPid(data.id,keywork);
                int i = ll_categoryRecord.indexOfChild(v);
                i = i + 1;
                List<View> childList = new ArrayList<View>();
                for (; i < ll_categoryRecord.getChildCount(); i++) {
                    childList.add(ll_categoryRecord.getChildAt(i));
                }
                for (int j = 0; j < childList.size(); j++) {
                    View view = childList.get(j);
                    if (view != null) {
                        ll_categoryRecord.removeView(childList.get(j));
                    }
                }
            }
        });
        ll_categoryRecord.setVisibility(View.VISIBLE);
        ll_categoryRecord.addView(view);

    }

    public void animInView(String pid, String name,String brandId,String keyword) {
        setInitData(pid,name,brandId,keyword);
        setVisibility(View.VISIBLE);
        if (getMeasuredWidth() == 0)
            return;
        final ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "x", getMeasuredWidth(), 0f);
        objectAnimator.setDuration(300);
        x.task().post(new Runnable() {
            @Override
            public void run() {
                objectAnimator.start();
            }
        });
    }

    public void animOutView() {
        if (getMeasuredWidth() == 0)
            return;
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "x", 0f, getMeasuredWidth());
        objectAnimator.setDuration(300);
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        objectAnimator.start();
    }

    String pid;
    FiltrateCategoryResponseBean resultData;
    BaseParentActivity context;
    boolean exeOne = true;
    FiltrateCategoryRequestFilter filter;

    public void setPid(String pid,String keyword) {
        cate_id = pid;
        this.pid = pid;
        if (filter == null) {
            filter = new FiltrateCategoryRequestFilter((BaseParentActivity) getContext());
        }
        filter.requestBean.pid = pid;
        if (!TextUtils.isEmpty(brandId))
        {
            filter.requestBean.brand_id=brandId;
        }
        filter.requestBean.keywords=keyword;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<FiltrateCategoryResponseBean>() {

            @Override
            public void onSuccess(FiltrateCategoryResponseBean result) {
                //解析result中的数据
                uiData = ReflectToBeanUtils.reflectArrayToBean(CateBean.class, result.getData(), 0);
                myadapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }

    List<CateBean> uiData = new ArrayList<>();


    class Myadapter extends BaseAdapter {

        @Override
        public int getCount() {
            return uiData.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(getContext(), R.layout.adapter_catagoryview, null);
            }
            CateBean bean = uiData.get(position);
            TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            tv_name.setText(bean.name);


            ImageView iv_rightArrow = (ImageView) convertView.findViewById(R.id.iv_rightArrow);
            ImageView iv_check = (ImageView) convertView.findViewById(R.id.iv_check);
            if (bean.hasChild == 1) {
                iv_rightArrow.setVisibility(View.VISIBLE);
            } else {
                iv_rightArrow.setVisibility(View.GONE);
            }
            if (TextUtils.equals(cate_id, bean.id)) {
                iv_check.setVisibility(View.VISIBLE);
            } else {
                iv_check.setVisibility(View.GONE);
            }

            return convertView;
        }
    }

    public interface SelectClickedListener {
        public void onClicked(String pid, String text);
    }

    SelectClickedListener selectListener;

    public void setSelectClickedListener(SelectClickedListener selectListener) {
        this.selectListener = selectListener;
    }
}
